package org.dei.test.llqruntime.oldtests;

import java.util.Calendar;
import java.util.Random;

import org.dei.perla.parser.clauses.OnUnsupportedSampleRateOptions;
import org.dei.perla.parser.expressions.ConstantBlob;
import org.dei.perla.parser.expressions.ConstantID;
import org.dei.perla.parser.expressions.ConstantTimestamp;
import org.dei.perla.sys.query.llqruntime.datastructures.QueryRecordStructure;
import org.dei.perla.sys.query.llqruntime.datastructures.Record;
import org.dei.perla.utils.pipe.Pipe;

public class FakeDevice extends Thread {
	
	private long pTimeToSleep = 500;
	
	private long pMaxFreq = 7000;
	
	private Pipe<Record> pPipeOut;
	
	public FakeDevice() {
		this.pPipeOut = new Pipe<Record>("FakeDevice pipe out");
		this.pPipeOut.start();
	}
	
	public Pipe<Record> getPipe() {
		return this.pPipeOut;
	}
	
	//...setter della nuova frequenza di sampling
	public synchronized boolean setSamplingFrequency(long parTime, OnUnsupportedSampleRateOptions parBehaviour) {
		
		if (parBehaviour == OnUnsupportedSampleRateOptions.SLOW_DOWN){
			
			if (parTime > pMaxFreq)
				this.pTimeToSleep = pMaxFreq;
			else
				this.pTimeToSleep= parTime;
			return true;
		}
		else
		{
			if (parTime > pMaxFreq){
				// Cosa fare? Siamo nel caso do not sample
				// Inizio ad impostarlo in questo modo per far smettere il thread produttore
				this.pTimeToSleep = -1;
				return false;
			} else {
				this.pTimeToSleep = parTime;
				return true;
			}
		}
		
	}
	
	private synchronized long getTimeToSleep() {
		
		return this.pTimeToSleep;
	}
	
	public void run() {
		
		QueryRecordStructure tRecordStructure = new QueryRecordStructure();
		tRecordStructure.addFieldStructure("ID_NODO", ConstantID.class);
		tRecordStructure.addFieldStructure("TIMESTAMP", ConstantTimestamp.class);
		tRecordStructure.addFieldStructure("DATA", ConstantBlob.class);
				
		Record tRecord = new Record(tRecordStructure);
		
		long timeToSleep;
		
		while (true) {
			
			System.out.println("Produced!");
			
			try {
				timeToSleep = this.getTimeToSleep();
				System.out.println("Prossimo campione tra...:" + timeToSleep);
				if (timeToSleep == -1){
					System.out.println("Frequenza di campionamento troppo alta, " +
							"smetto di produrre record!!!");
					// Chiudo la pipe
					pPipeOut.enqueue(null);
					break;
				}
				sleep(timeToSleep);
				System.out.println("===============================================");
				//Non ha senso Random per ID, è solo per testare facendo minime modifiche 
				//serviva solo per testare WHERE
				Random tRandom = new Random();
				tRecord.setField("ID_NODO", new ConstantID(tRandom.nextInt(4)));
				tRecord.setField("TIMESTAMP", new ConstantTimestamp(Calendar.getInstance().getTimeInMillis()));
				Float tFloat = new Float(Math.random());
				tRecord.setField("DATA", new ConstantBlob(tFloat.toString().getBytes()));
				pPipeOut.enqueue(tRecord);
				
			} catch (InterruptedException e) {
				e.printStackTrace();
				System.out.println("===============================================");
			}
		}
	}

}